package StudentProject.controller;

import StudentProject.bean.Student;
import StudentProject.bean.User;
import StudentProject.dao.GradesDaoImp;
import StudentProject.dao.StudentDaoImp;
import StudentProject.dao.UserDaoImp;
import StudentProject.view.View;

import java.util.Scanner;

public class Control {
    private static UserDaoImp userDaoImp = new UserDaoImp();
    private static StudentDaoImp studentDaoImp = new StudentDaoImp();
    private static boolean flage;

    public static void main(String[] args) {
        User user = View.indexView();
        while (true) {
            //给用户展示登录界面
            int type = userDaoImp.login(user);
            switch (type) {
                case 0:
                    System.out.println("输入错误，请重试");
                    break;
                case 1:
                    System.out.println("欢迎登录管理员界面");
                    managerService();
                    break;
                case 2:
                    System.out.println("欢迎登录学生界面!");
                    studentService();
                    break;
                default:
                    break;
            }
        }
    }

    //管理员页面方法
    private static void managerService() {
        while (true) {
            int item = View.managerMenuView();
            switch (item) {
                case 0:
                    System.out.println("退出成功！");
                    System.exit(-1);
                    break;
                case 1:
                    //学生账号管理系统
                    studentAccountService();
                    break;
                case 2:
                    //学生信息管理系统
                    studentInfoService();
                    break;
                case 3:
                    //学生成绩管理系统
                    studentGradeService();
                    break;
            }

        }
    }

    //学生账号管理界面方法
    private static void studentAccountService() {
        while (true) {
            int item = View.studentAccountMenuView();
            switch (item) {
                case 0:
                    System.out.println("退出成功！");
                    managerService();
                    break;
                case 1:
                    //添加学生账号
                    User userAdd = View.addMenuView();
                    flage = userDaoImp.insert(userAdd);
                    System.out.println(flage ? "添加成功" : "添加失败");
                    break;
                case 2:
                    //删除学生账号
                    String userDelete = View.deleteMenuView();
                    flage = userDaoImp.delete(userDelete);
                    System.out.println(flage ? "删除成功" : "删除失败");
                    break;
                case 3:
                    //修改学生账号
                    User userUpdate = View.updateMenuView();
                    flage = userDaoImp.update(userUpdate);
                    System.out.println(flage ? "修改成功" : "修改失败");
                    break;
                case 4:
                    //查询学生账号
                    String userSelect = View.selectMenuView();
                    User user = userDaoImp.select(userSelect);
                    View.printUserInfo(user);
                    break;
            }
        }
    }

    //学生信息管理系统
    public static void studentInfoService() {
        while (true) {
            int item = View.studentInfoMenuView();

            switch (item) {
                case 0:
                    System.out.println("退出成功！");
                    managerService();
                    break;
                case 1:
                    //添加学生信息
                    Student studentAdd = View.addStudentInfoMenuView();
                    flage = studentDaoImp.insert(studentAdd);
                    System.out.println(flage ? "添加成功" : "添加失败");
                    break;
                case 2:
                    //删除学生信息
                    Student studentDelete = View.deleteStudentMenuView();
                    break;
                case 3:
                    //修改学生信息
                    Student studentUpadate = View.updateStudentInfoMenuView();
                    StudentDaoImp studentDaoImp = new StudentDaoImp();
                    flage = studentDaoImp.update(studentUpadate);
                    System.out.println(flage ? "修改成功" : "修改失败");
                    break;
                case 4:
                    //查询学生信息
                    View.selectStudentMenuView();
                    break;
            }
        }
    }

    //学生成绩管理系统
    public static void studentGradeService() {

        Scanner scanner = new Scanner(System.in);
        GradesDaoImp gradesDaoImp = new GradesDaoImp();
        while (true) {
            int item = View.studentGradeService();
            switch (item) {
                case 0:
                    System.out.println("退出成功！");
                    managerService();
                    break;
                case 1:
                    gradesDaoImp.addGrade(scanner);
                    break;
                case 2:
                    gradesDaoImp.deleteGrade(scanner);
                    break;
                case 3:
                    gradesDaoImp.updateGrade(scanner);
                    break;
                case 4:
                    gradesDaoImp.queryGrade(scanner);
                    break;
                default:
                    System.out.println("无效的选择，请重新输入。");
                    studentGradeService();
            }
        }

    }


    //学生界面
    public static void studentService() {

        Scanner scanner = new Scanner(System.in);
        while (true) {
            StudentDaoImp studentDaoImp = new StudentDaoImp();
            int item = View.studentService();
            switch (item) {
                case 0:
                    System.out.println("退出成功！");
                    System.exit(-1);
                    break;
                case 1:
                    //
                    studentDaoImp.queryGrade(scanner);
                    break;
                default:
                    System.out.println("输入错误重新输入");
                    studentService();
                    break;
            }
        }
    }
}